package disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import disruptor.event.ValueEvent;
import disruptor.event.handler.ValueEventHandler;
import disruptor.factory.ValueEventFactory;

import java.util.concurrent.Executors;

/**
 * @ClassName DisruptorExample
 * @PackageName disruptor
 * @projectName myThread
 * @Description 示例展示 Disruptor 基本用法
 * @Author games
 * @Date 2025/9/8 下午4:56
 * @Version 1.0
 */
public class DisruptorExample {

    public static void main(String[] args) {

        // 创建 Disruptor
        Disruptor<ValueEvent> disruptor = new Disruptor<>(
                new ValueEventFactory(),     // 事件工厂
                1024,                        // Ring Buffer 大小
                Executors.defaultThreadFactory() // 线程工厂
        );

        // 连接事件处理器
        disruptor.handleEventsWith(new ValueEventHandler());

        // 启动 Disruptor
        disruptor.start();

        // 获取 Ring Buffer
        RingBuffer<ValueEvent> ringBuffer = disruptor.getRingBuffer();

        // 生产事件
        long sequence = ringBuffer.next();
        try {
            ValueEvent event = ringBuffer.get(sequence);
            event.setValue(12345L);
        } finally {
            ringBuffer.publish(sequence);
        }

        // 关闭 Disruptor
        disruptor.shutdown();
    }





}
